package me.zhengjie.lift;

import lombok.RequiredArgsConstructor;
import me.zhengjie.nettydemo.lift.LiftStatusVo;
import me.zhengjie.nettydemo.mapper.LiftNettyMapper;
import me.zhengjie.pojo.entity.Lift;
import me.zhengjie.pojo.entity.LiftTask;
import me.zhengjie.pojo.entity.LiftWaitPoint;
import me.zhengjie.sqlservilce.LiftSqlService;
import me.zhengjie.sqlservilce.LiftTaskSqlService;
import me.zhengjie.sqlservilce.LiftWaitPointService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Component
@RequiredArgsConstructor
public class LiftWaitPointTiming {
    public static final Logger liftLog = LoggerFactory.getLogger("lift");

    private final LiftWaitPointService liftWaitPointService;

    private final LiftTaskSqlService liftTaskService;

    private final LiftSqlService liftSqlService;

    private final LiftNettyMapper liftNettyMapper;

    @Transactional
    @Scheduled(fixedDelay = 1000)
    public void liftWaitPoint() {
        try {
            //查询电梯等待点，创建电梯任务
            List<LiftWaitPoint> liftWaitPointList = liftWaitPointService.selectAll();
            if (!liftWaitPointList.isEmpty()) {
                for (LiftWaitPoint liftWaitPoint : liftWaitPointList) {
                    String liftCode = liftWaitPoint.getLiftCode();
                    //电梯储位AGV模式才能生成任务
                    Lift lift = liftSqlService.selectLiftCode(liftCode);
                    if (lift !=null){
                        Integer liftMode = lift.getLiftMode();
                        String liftStorage = lift.getLiftStorage();
                        if (liftMode == 1){
                            //查询电梯是否有任务
                            LiftTask liftTask = liftTaskService.selectLiftCode(liftCode);
                            if (liftTask == null) {
                                //todo 查询电梯所在楼层
                                LiftStatusVo liftInfo = liftNettyMapper.findLiftInfo(liftCode);
                                liftLog.info("等待任务中,查询出电梯状态：{}", liftInfo);
                                int currentFloor = liftInfo.getCurrentFloor();
                                String floor = String.valueOf(currentFloor);
                                //根据电梯所在楼层，查当前所在楼层创建任务
                                List<LiftWaitPoint> waitPointList = liftWaitPointService.selectElevatorFloorTask(floor);
                                if (!waitPointList.isEmpty()) {
                                    LiftWaitPoint liftWaitPointTask = waitPointList.get(0);
                                    Integer id = liftWaitPointTask.getId();
                                    String taskCode = liftWaitPointTask.getTaskCode();
                                    String startFloor = liftWaitPointTask.getStartFloor();
                                    String endFloor = liftWaitPointTask.getEndFloor();
                                    LiftTask liftTaskInsert = new LiftTask();
                                    liftTaskInsert.setTaskCode(taskCode);
                                    liftTaskInsert.setLiftCode(liftCode);
                                    liftTaskInsert.setStartFloor(startFloor);
                                    liftTaskInsert.setEndFloor(endFloor);
                                    liftTaskInsert.setStatus("0");
                                    liftTaskInsert.setCreateTime(LocalDateTime.now());
                                    liftTaskInsert.setUpdateTime(LocalDateTime.now());
                                    liftTaskInsert.setLiftStorage(liftStorage);
                                    //创建当前楼层任务
                                    boolean b = liftTaskService.insertLiftTask(liftTaskInsert);
                                    if (!b) {
                                        liftLog.error("创建电梯任务失败，数据回滚");
                                        throw new RuntimeException("创建电梯任务失败，数据回滚");
                                    }
                                    //删除请求任务
                                    boolean b1 = liftWaitPointService.removeId(id);
                                    if (!b) {
                                        liftLog.error("删除等待点任务失败，数据回滚");
                                        throw new RuntimeException("删除等待点任务失败，数据回滚");
                                    }
                                } else {
                                    //跨楼层任务创建 根据请求时间最早创建任务
                                    List<LiftWaitPoint> liftWaitPoints = liftWaitPointService.selectLiftCode(liftCode);
                                    if (!liftWaitPoints.isEmpty()) {
                                        LiftWaitPoint liftWaitPointTask = liftWaitPoints.get(0);
                                        Integer id = liftWaitPointTask.getId();
                                        String taskCode = liftWaitPointTask.getTaskCode();
                                        String startFloor = liftWaitPointTask.getStartFloor();
                                        String endFloor = liftWaitPointTask.getEndFloor();
                                        LiftTask liftTaskInsert = new LiftTask();
                                        liftTaskInsert.setTaskCode(taskCode);
                                        liftTaskInsert.setLiftCode(liftCode);
                                        liftTaskInsert.setStartFloor(startFloor);
                                        liftTaskInsert.setEndFloor(endFloor);
                                        liftTaskInsert.setStatus("0");
                                        liftTaskInsert.setCreateTime(LocalDateTime.now());
                                        liftTaskInsert.setUpdateTime(LocalDateTime.now());
                                        //创建当前楼层任务
                                        boolean b = liftTaskService.insertLiftTask(liftTaskInsert);
                                        if (!b) {
                                            liftLog.error("创建电梯任务失败，数据回滚");
                                            throw new RuntimeException("创建电梯任务失败，数据回滚");
                                        }
                                        //删除请求任务
                                        boolean b1 = liftWaitPointService.removeId(id);
                                        if (!b) {
                                            liftLog.error("删除等待点任务失败，数据回滚");
                                            throw new RuntimeException("删除等待点任务失败，数据回滚");
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }

    }
}
